package com.leyou.sms.lisener;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.leyou.sms.config.SmsProperties;
import com.leyou.sms.utils.SmsUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 项目: leyou
 *
 * @author 岩冰
 * @version 2018/10/17 13:04
 */
@Component
@EnableConfigurationProperties(SmsProperties.class)
public class SmsListener {


    @Autowired
    private SmsUtils smsUtils;


    @Autowired
    private SmsProperties prop;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "ly.sms.queue",durable = "true"),
    exchange = @Exchange(value = "ly.sms.exchange",ignoreDeclarationExceptions = "true",type = ExchangeTypes.TOPIC),
    key = {"sms.verify.code"}))
    public void listenSms(Map<String,String>msg) throws ClientException {
        if (msg==null||msg.isEmpty()) {
            return;
        }
        String phone = msg.get("phone");
        String code = msg.get("code");
        if (StringUtils.isBlank(phone)||StringUtils.isBlank(code)) {
            return;
        }
        SendSmsResponse resp = this.smsUtils.sendSms(phone, code, prop.getSignName(), prop.getVerifyCodeTemplate());
        if (!"OK".equals(resp.getMessage())) {
            throw new RuntimeException();
        }
    }
}
